package contact.controller;

import java.util.List;

import model.db.Query;
import model.db.User;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import contact.model.Contact;
import contact.util.HibernateUtil;

public class QueryManager {
	public User add(User user) {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(user);
		session.getTransaction().commit();
		return user;
	}
	
	public User delete(Long id) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		User contact = (User) session.load(User.class, id);
		if(null != contact) {
			session.delete(contact);
		}
		session.getTransaction().commit();
		return contact;
	}
	
	
	public List<Query> list() {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Query> queries = null;
		try {
			
			queries = (List<Query>)session.createQuery("from Query  where active=1").list();
			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return queries;
	}
	
public List<Query> list(int active) {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Query> queries = null;
		try {
			
			queries = (List<Query>)session.createQuery("from Query  where active="+ active).list();
			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return queries;
	}
	
	
	
	
	
	
}
